package com.fm.financemanage.utils;


public class CosineSimilarityUtils {
    public static double calculateCosineSimilarity(double[] vector1, double[] vector2) {
        if (vector1.length != vector2.length) {
            throw new IllegalArgumentException("Vector sizes should be equal");
        }

        double dotProduct = 0.0;
        double normVector1 = 0.0;
        double normVector2 = 0.0;

        for (int i = 0; i < vector1.length; i++) {
            dotProduct += vector1[i] * vector2[i];
            normVector1 += Math.pow(vector1[i], 2);
            normVector2 += Math.pow(vector2[i], 2);
        }

        normVector1 = Math.sqrt(normVector1);
        normVector2 = Math.sqrt(normVector2);

        return dotProduct / (normVector1 * normVector2);
    }
}
